package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.AlipayException;
import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.exception.sql.UpdateException;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.common.utils.OrdersUtil;
import com.fantastic.mapper.zjs.AccountCouponsMapper;
import com.fantastic.mapper.zjs.AccountMapper;
import com.fantastic.mapper.zjs.OrdersMapper;
import com.fantastic.mapper.zjs.UserMapper;
import com.fantastic.model.pojo.zjs.Account;
import com.fantastic.model.pojo.zjs.Orders;
import com.fantastic.model.pojo.zjs.User;
import com.fantastic.service.zjs.OrderDetailsService;
import com.fantastic.service.zjs.OrdersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/6/5 20:29
 * @description: 订单信息表service接口实现类
 */
@Service
public class OrdersServiceImpl implements OrdersService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    OrderDetailsService orderDetailsService;
    @Resource
    UserMapper userMapper;
    @Resource
    AccountMapper accountMapper;
    @Resource
    AccountCouponsMapper accountCouponsMapper;

    /**
     * 查询订单信息
     *
     * @param orders
     * @return
     */
    @Override
    public List<Orders> listOrders(Orders orders) {
        List<Orders> ordersList = ordersMapper.listOrders(orders);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), ordersList);
        return ordersList;
    }

    /**
     * 查询单个订单信息
     *
     * @param orderId
     * @return
     */
    @Override
    public Orders getOrders(String orderId) {
        IsEmptyUtils.stringIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR), orderId);
        return ordersMapper.getOrders(orderId);
    }

    /**
     * 查询倒序后的订单编号
     *
     * @return
     */
    @Override
    public String getOrders() {
        return ordersMapper.getMaxOrders();
    }

    /**
     * 添加订单信息
     *
     * @param orders
     * @return
     */
    @Override
    @Transactional
    public Integer insertOrders(Orders orders, Integer couponsId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR), orders.getAccountId(), orders.getOrderWay(), orders.getOrderStatus());
        IsEmptyUtils.objectIsNull(new SelectException(RespStatusInfo.DATA_ERROR), orders.getOrderPays());
        //设置订单时间
        orders.setOrderTime(LocalDateTime.now());
        //获取最大的订单编号
        String maxOrderId = getOrders();
        //生成新的最大订单编号，填充到Orders对象中
        orders.setOrderId(OrdersUtil.getOrdersId(maxOrderId));
        //将购物车的信息添加到订单详情。添加成功后删除购物车的信息
        orderDetailsService.addsOrderDetails(orders);
        //获取用户账号余额
        Account getAccount = accountMapper.getAccount(orders.getAccountId());
        //小于零，表示后者大于前者
        if (getAccount.getAccountStored().compareTo(orders.getOrderPays()) < 0) {
            //抛出余额不足异常
            throw new AlipayException(RespStatusInfo.MONEY_NOT);
        }
        //填充新钱
        getAccount.setAccountStored(
                getAccount.getAccountStored().subtract(orders.getOrderPays()));
        //进行余额修改
        accountMapper.updateAccount(getAccount);

        //获取原有的用户信息
        User user = userMapper.getUser(getAccount.getUserId());
        //填充新积分
        user.setUserIntegral(user.getUserIntegral() + Integer.valueOf(String.valueOf(getAccount.getAccountStored())));
        //修改账号积分
        userMapper.updateUser(user);

        //不为null
        if (couponsId != null) {
            //删除优惠卷
            accountCouponsMapper.deleteAccountCoupon(orders.getAccountId(), couponsId);
        }
        //添加订单信息
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改订单信息
     *
     * @param orders
     * @return
     */
    @Override
    public Integer updateOrders(Orders orders) {
        IsEmptyUtils.stringIsEmpty(new UpdateException(RespStatusInfo.DATA_ERROR), orders.getOrderId());
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 删除订单信息
     *
     * @param orderId
     * @return
     */
    @Override
    public Integer deleteOrders(Integer orderId) {
        IsEmptyUtils.integerIsEmpty(new UpdateException(RespStatusInfo.DATA_ERROR), orderId);
        return ordersMapper.deleteOrders(orderId);
    }
}
